#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <string>

using namespace std;

class Request
{
public:
    Request(int start,int end,const string& threadname)
    :_start(start)
    ,_end(end)
    ,_threadname(threadname)
    {}

public:
    int _start;
    int _end;
    string _threadname;
};

class Response
{
public:
    Response(int retval,int retcode)
    :_retval(retval)
    ,_retcode(retcode)
    {}
public:
    int _retval;
    int _retcode;
};

void* sumcount(void* argc)
{
    Request* rq = static_cast<Request*>(argc);
    Response* rsp = new Response(0,0);
    for(int i = rq->_start;i <= rq->_end;i++)
    {
        rsp->_retval += i;
    }

    delete rq;
    exit(11);
    return rsp;
}

int main()
{
    pthread_t tid;
    Request* rq = new Request(1,100,"mythread");
    pthread_create(&tid,nullptr,sumcount,rq);//这里为什么不用强制类型转化

    void* ret;
    pthread_join(tid,&ret);
    Response *rsp = static_cast<Response *>(ret);
    cout << "rsp->result: " << rsp->_retval << ", exitcode: " << rsp->_retcode << endl;
    delete rsp;
    return 0;
}


// int num = 100;

// void* newthread(void* arr)
// {
//     const char* name = (const char*)arr;
//     int cnt = 5;
//     while(1)
//     {
//         cout << name << "new thread : pid : " << getpid() << " num : " << num << endl;
//         sleep(1);
//         cnt--;
//         if(cnt == 0) break;
//     }
//     return (void*)1;
// }

// int main()
// {
//     pthread_t tid;
//     pthread_create(&tid,nullptr,newthread,(void*)"Thread 1");    

//     // while(1)
//     // {
//     //     cout << "main thread : pid : " << getpid() << endl;
//     //     sleep(1);
//     //     num++;
//     // }
//     sleep(7);
//     void* retval;
//     pthread_join(tid,&retval);
//     cout << "ret : " << (long)retval << endl;
//     cout << "main quit success ..." << endl;
//     return 0;
// }